import QtQuick 2.0
import "../components"
import "../sampleManager"


/* 数据分析展示、筛选控制 */
Item {

    id: root



    // public ===================================================================================================
    // 切换当前设备Id
    function tabAnalysis(equId){
        root.equId = equId
        CAnalysis.openAnalysis(equId)
        refresh()
    }
    function close(equId){
        CAnalysis.closeAnalysis(equId)
    }


    // private =====================================================================================================

    // 设备标识
    property string equId: ""

    // 页面信息
    property int  curChart: 0        // (曲线的页面)
    property int  curPage: 0

    property bool canAnalysis: true
    property bool canGuide: true

    // 模型配置
    property int modelFunc: 0
    property int modelCt: 0         // [绝对阈值]-1 [另外两种算法忘了是多少了..]
    property int modelFilter: 0
    property string modelIR: ""

    property bool   mutexPage: false      // 在从pState同步页面时上锁，不触发状态变更造成的页面更新

    property var    channelName: ["FAM", "VIC", "ROX", "CY5"]

    // 标准曲线
    property var standardInfo: ({})

    // 刷新调用
    function reloadAnalysisData(){      // 用于子组件修改后端数据时的数据刷新
        refresh()
    }function reloadChartData(){
        pcrChart.refresh()
    }


    // 刷新信息 (后端-->前端)
    function refresh(){ // 每次变更状态都刷新全部数据

        mutexPage = true

        // 页面选择刷新
        refreshPageState()
        refreshChannelChecked()
        refreshModelState()

        // 当前页面的信息刷新
        standardInfo = CAnalysis.getStandardInfo()

        // 图表刷新
        guideTable.refresh(0)
        pcrTable.refresh()
        pcrChart.refresh()

        let stateInfo = CAnalysis.getAnalysisState()
        canAnalysis = stateInfo["CanAnalysis"]
        canGuide = stateInfo["CanGuide"]

        mutexPage = false

    }

    function analysisConfig(){
        boxAnalysisConfig.show()
    }

    function refreshChannelChecked(){

        let channels = CAnalysis.getFilterList_channel()
        checkChannel1.check = channels.indexOf(channelName[0])!==-1
        checkChannel2.check = channels.indexOf(channelName[1])!==-1
        checkChannel3.check = channels.indexOf(channelName[2])!==-1
        checkChannel4.check = channels.indexOf(channelName[3])!==-1

    }

    function refreshPageState(){

        curPage = CAnalysis.getCurrentPage()
        curChart = CAnalysis.getCurrentChart()

        checkRn.check = false
        checkLog.check = false
        checkNorm.check = false

        if(curChart === 0){                 // RN
            checkRn.check = true
        }else if(curChart === 1){           // DELTA_RN

        }else if(curChart === 2){           // LOG_RN
            checkRn.check = true
            checkLog.check = true
        }else if(curChart === 3){           // LOG_DELTA_RN
            checkLog.check = true
        }else if(curChart === 4){           // NORM
            checkNorm.check = true
        }
    }

    function refreshModelState(){
        var modelMap = CAnalysis.getModelConfig()
        modelFilter = modelMap["Filter"]
        modelFunc = modelMap["Func"]
        modelCt = modelMap["Ct"]
        modelIR = modelMap["IR"]
        if(modelCt >= 0){
            checkThr.check = false
        }
    }


    // 刷新信息 ( 前端 --> 后端 ) =============================

    // 用户切换界面或CheckBox时触发
    function updateCurPage(pageIdx){

        if(mutexPage) { return }
        CAnalysis.changePage(pageIdx)
        refresh()

    }
    function updateCurChart(chartIdx){

        if(mutexPage) { return }

        if(checkNorm.check){        // NORM
            chartIdx = 4
        }else if(checkLog.check && checkRn.check){   // LOG_RN
            chartIdx = 2
        }else if(checkLog.check){                    // LOG_DELTA_RN
            chartIdx = 3
        }else if(checkRn.check){                     // RN
            chartIdx = 0
        }else{                                       // DELTA_RN
            chartIdx = 1
        }
        CAnalysis.changeChart(chartIdx)
        refresh()

    }

    // 更新筛选
    function updateFilter(){
        if(mutexPage) { return }

        var channelList = []

        if(checkChannel1.check){
            channelList.push(channelName[0])
        }if(checkChannel2.check){
            channelList.push(channelName[1])
        }if(checkChannel3.check){
            channelList.push(channelName[2])
        }if(checkChannel4.check){
            channelList.push(channelName[3])
        }

        CAnalysis.updateFilter_channel(channelList)

        refresh()

    }

    // 更新阈值线是否显示
    function updateThreshold(){
        CAnalysis.setThresholdVisible(checkThr.check)
        refresh()
    }


    // 界面样式 ===============================================================================================

    onVisibleChanged: {
        if(visible){
            tabAnalysis(root.equId)
        }
    }
    BoxAnalysisConfig{
        id: boxAnalysisConfig
        onAccept: {
            refresh()
        }
    }
    BoxFilter{
        id: boxFilter
        onAccept: {
            root.refresh()
        }
    }


    anchors.fill: parent
    Rectangle{          // 上面的行
        height: 60*dp
        width: root.width
        PushButton{
            height: 40*dp;  width: 80*dp
            y: 20*dp;  x: 10*dp
            title: "原始曲线"
            underlineSelect: true
            isSelected: curPage === 0
            onReleased: {
                updateCurPage(0)
            }
        }PushButton{
            height: 40*dp;  width: 80*dp
            y: 20*dp;  x: 110*dp
            title: "扩增曲线"
            underlineSelect: true
            isSelected: curPage === 1
            onReleased: {
                updateCurPage(1)
            }
        }PushButton{
            height: 40*dp;  width: 80*dp
            y: 20*dp;  x: 210*dp
            title: "标准曲线"
            underlineSelect: true
            isSelected: curPage === 2
            onReleased: {
                updateCurPage(2)
            }
        }PushButton{
            height: 40*dp;  width: 90*dp
            y: 20*dp;  x: 310*dp
            title: "下游操作建议"
            underlineSelect: true
            isSelected: curPage === 3
            onReleased: {
                updateCurPage(3)
            }
        }
        CustomCheckBox{
            id: checkRn
            box_text: "Rn"
            x: root.width/2 - 20*dp;  y: 20*dp
            visible: curPage === 1 || (curPage === 2 && modelCt === -1)
            onClicked: {
                checkNorm.check = false
                updateCurChart()
            }
        }CustomCheckBox{
            id: checkLog
            box_text: "对数"
            x: root.width/2 + 60*dp;  y: 20*dp
            visible: curPage === 1 || (curPage === 2 && modelCt === -1)
            onClicked: {
                checkNorm.check = false
                updateCurChart()
            }
        }CustomCheckBox{
            id: checkNorm
            box_text: "归一化"
            x: root.width/2 + 140*dp;  y: 20*dp
            visible: curPage === 1 || (curPage === 2 && modelCt === -1)
            checkable: channelName.indexOf(modelIR) !== -1
            onClicked: {
                checkRn.check = false
                checkLog.check = false
                updateCurChart()
            }onClicked_disabled: {
                mainwindow.showToast("该功能仅在内标生效时可用", 2)
            }
        }CustomCheckBox{
            id: checkThr
            box_text: "显示阈值线"
            x: root.width/2 + 240*dp;  y: 20*dp
            visible: curPage === 1
            checkable: modelCt === -1
            onClicked: {
                updateThreshold()
            }onClicked_disabled: {
                mainwindow.showToast("该功能仅在绝对阈值时可用", 2)
            }
        }

        PushButton{
            y: 10*dp;  x: root.width - 140*dp
            title: "筛选"
            defaultMode2: true
            onReleased: {
                boxFilter.show()
            }
        }

        CustomBorder{
            bBorderwidth: 1
            borderColor: "#CCCCCC"
        }
    }


    property real centerLineX: root.width / 2
    Item{      // 图表

        y: 90*dp; x: 30*dp
        height: root.height-150*dp
        width: centerLineX - 60*dp
        visible: curPage !== 3


        Item{           // 图表上面的筛选
            height: 40*dp; width: parent.width
            Row{
                anchors.fill: parent
                CustomCheckBox{
                    id: checkChannel1
                    box_text: channelName[0]
                    fontColor: config.colorChannel1
                    check: true
                    onCheckChanged: {
                        updateFilter()
                    }
                }CustomCheckBox{
                    id: checkChannel2
                    box_text: channelName[1]
                    fontColor: config.colorChannel2
                    check: true
                    onCheckChanged: {
                        updateFilter()
                    }
                }CustomCheckBox{
                    id: checkChannel3
                    box_text: channelName[2]
                    fontColor: config.colorChannel3
                    check: true
                    onCheckChanged: {
                        updateFilter()
                    }
                }CustomCheckBox{
                    id: checkChannel4
                    box_text: channelName[3]
                    fontColor: config.colorChannel4
                    check: true
                    onCheckChanged: {
                        updateFilter()
                    }
                }
            }
            Text {
                id: text_IR
                text: "当前内标: " + (modelIR==="" ? "无" : modelIR)
                x: parent.width - contentWidth
                anchors.verticalCenter: parent.verticalCenter
                font.pixelSize: config.fontsize
                color: "#999999"
            }
        }


        Item{
            y: 50*dp
            height: parent.height - (curPage===2 ? 110*dp : 50*dp); width: parent.width
            PCRChart{
                id: pcrChart
            }
        }Item{          // 图表下面的Legend
            y: parent.height - 60*dp;
            height: 60*dp; width: parent.width
            visible: curPage === 2
            Grid{
                columns: 2
                anchors.fill: parent
                spacing: 0
                Item{
                    height: parent.height/2; width: parent.width/2
                    Text{
                        id: text_channelInof1
                        text: "— " + channelName[0] + "  E:" + standardInfo["0"]["E"] + "  K:" + standardInfo["0"]["K"] + "  R\xB2:" + standardInfo["0"]["R2"] + "  y-int:" + standardInfo["0"]["Y"]
                        color: config.colorChannel1
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                }Item{
                    id: text_channelInof2
                    height: parent.height/2; width: parent.width/2
                    Text{
                        text: "— " + channelName[1] + "  E:" + standardInfo["1"]["E"] + "  K:" + standardInfo["1"]["K"] + "  R\xB2:" + standardInfo["1"]["R2"] + "  y-int:" + standardInfo["1"]["Y"]
                        color: config.colorChannel2
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                }Item{
                    id: text_channelInof3
                    height: parent.height/2; width: parent.width/2
                    Text{
                        text: "— " + channelName[2] + "  E:" + standardInfo["2"]["E"] + "  K:" + standardInfo["2"]["K"] + "  R\xB2:" + standardInfo["2"]["R2"] + "  y-int:" + standardInfo["2"]["Y"]
                        color: config.colorChannel3
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                }Item{
                    id: text_channelInof4
                    height: parent.height/2; width: parent.width/2
                    Text{
                        text: "— " + channelName[3] + "  E:" + standardInfo["3"]["E"] + "  K:" + standardInfo["3"]["K"] + "  R\xB2:" + standardInfo["3"]["R2"] + "  y-int:" + standardInfo["3"]["Y"]
                        color: config.colorChannel4
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                }
            }
        }

    }

    // 图表和表格中间可以拉的那个线
    Rectangle{
//        x: centerLineX;  y: 80*dp
//        height: root.height - 100*dp;  width: 1
//        color: "#CCCCCC"
    }

    Item{          // 表格
        y: 80*dp
        x: centerLineX + 20*dp
        height: root.height - 100*dp
        width: root.width / 2 - 40*dp
        PCRTable{
            id: pcrTable
        }
        visible: curPage !== 3
    }
    Item{        // 指导建议
        y: 70*dp
        x: 10*dp
        height: root.height - 80*dp
        width: root.width - 20*dp
        GuideTable{
            id: guideTable
            type: 0
        }

        visible: curPage === 3
    }

}
